//
//  ToastHelper.m
//  JianDan
//
//  Created by 刘献亭 on 15/9/11.
//  Copyright (c) 2015年 刘献亭. All rights reserved.
//

#import "ToastHelper.h"
#import "MBProgressHUD.h"
#import "NSString+Additions.h"

@interface ToastHelper () <MBProgressHUDDelegate>

@property(nonatomic, strong) UIView *hudParentView;

@property(nonatomic, strong) UIView *toastHUDParentView;

@property(nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation ToastHelper

DEFINE_SINGLETON_IMPLEMENTATION(ToastHelper)

- (void)setUp {


}

- (void)setSimleProgressVisiable:(BOOL)simleProgressVisiable {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (simleProgressVisiable) {
            if(!_HUD){
                UIView *view = [UIApplication sharedApplication].delegate.window;
                self.HUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
                _HUD.delegate = self;
                _HUD.removeFromSuperViewOnHide = YES;
            }
//            [_HUD showAnimated:YES];
        } else {
            [_HUD hideAnimated:YES];
            _HUD = nil;
        }
    });
}

- (void)hudWasHidden:(MBProgressHUD *)hud {
    if (self.hudParentView && self.HUD == hud) {
        [self.HUD removeFromSuperview];
        [self.hudParentView removeFromSuperview];
        self.HUD = nil;
        self.hudParentView = nil;
        return;
    }
    if (self.toastHUDParentView) {
        [self.toastHUDParentView removeFromSuperview];
        self.toastHUDParentView = nil;
    }
}

/**
 *  再window上显示用户等待框加文字
 *
 *  @param textString 文字
#0	0x000a2298 in -[ToastHelper setUp] at /Users/shenhualxt/JianDanIOS/JianDan/Helper/LittleTools/ToastHelper.m:51
 *  @param delayTime 时间
 */
- (void)toast:(NSString *)textString delayTime:(int)delayTime {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *view = [UIApplication sharedApplication].delegate.window;
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        hud.animationType = MBProgressHUDAnimationZoomOut;
        hud.label.text = textString;
        hud.label.numberOfLines = 0;
        hud.margin = 10.0f;
//        hud.yOffset = 0.0f;
        hud.delegate = self;
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:delayTime];
    });
}

-(void)toastSuccess:(NSString *)message{
    [self toastWithMessge:message withImageName:@"ic_mb_hub_checkmark"];
}

-(void)toastError:(NSString *)message{
    [self toastWithMessge:message withImageName:@"ic_mb_hub_error"];
}

-(void)toastWithMessge:(NSString *)message withImageName:(NSString *)imageName{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *view = [UIApplication sharedApplication].delegate.window;
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        hud.customView = imageView;
        hud.mode = MBProgressHUDModeCustomView;
        hud.label.numberOfLines = 0;
        hud.label.text = message;
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:2.f];
    });
}

- (void)toast:(NSString *)textString {
    [self toast:textString delayTime:2];
}

@end
